package com.movitech.mobile.cz.base.sys.controller;


import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.vo.PageRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SMessagesReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.entity.SMessages;
import com.movitech.mobile.cz.base.sys.service.SMessagesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import java.util.List;

/**
 * <p>
 * SMessages 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-01
 */
@RestController
@RequestMapping("/v1/sMessages")
@Api(value = "消息通知基础操作接口", description = "消息通知基础操作接口")
public class SMessagesController {
    @Autowired
    @Qualifier("sMessagesService")
    private SMessagesService sMessagesService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有消息通知接口",
            notes = "获取所有消息通知接口",
            response = SMessages.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SMessages> page = sMessagesService.selectPage(new Page<SMessages>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/allMessageList")
    @ApiOperation(
            value = "个人消息中心列表接口",
            notes = "个人消息中心列表接口",
            response = SMessages.class
    )
    public RestReturnResult allMessageList(HttpServletRequest request, HttpServletResponse response,@RequestBody SMessagesReqVO sMessagesReqVO) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL,"", CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try{
            hResult=sMessagesService.allMessageList(request,response,sMessagesReqVO);
        }catch (RuntimeException e){
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL,"", CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        }
        return hResult;
    }

    @GetMapping("/readSMessage/{messageId}")
    @ApiOperation(
            value = "消息已读",
            notes = "消息已读",
            response = SMessages.class
    )
    public RestReturnResult readSMessage(HttpServletRequest request, HttpServletResponse response,@PathVariable String messageId) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL,"", CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try{
            hResult=sMessagesService.readSMessage(request,response,messageId);
        }catch (RuntimeException e){
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL,"", CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        }
        return hResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "消息通知列表查询接口",
            notes = "消息通知列表查询接口",
            response = SMessages.class
    )
    public RestReturnResult searchList(@RequestBody SMessages sMessages,
                @ApiParam(value = "分页页数")@PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SMessages> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sMessages);
        page = sMessagesService.selectPage(page,wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @GetMapping("/getSMessages/{id}")
    @ApiOperation(
            value = "消息通知对象ID获取接口",
            notes = "消息通知对对象ID获取接口",
            response = SMessages.class
    )
    public RestReturnResult addSMessages(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SMessages sMessages = sMessagesService.selectById(id);
        restReturnResult.setData(sMessages);
        return restReturnResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "消息通知添加接口",
            notes = "消息通知添加接口",
            response = SMessages.class
    )
    public RestReturnResult add(@RequestBody SMessages sMessages) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sMessagesService.insert(sMessages);
        restReturnResult.setData(sMessages);
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "消息通知更新接口",
            notes = "消息通知更新接口",
            response = SMessages.class
    )
    public RestReturnResult update(@RequestBody SMessages sMessages) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sMessagesService.updateById(sMessages);
        restReturnResult.setData(sMessages);
        return restReturnResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "消息通知通过ID删除接口",
            notes = "消息通知通过ID删除接口",
            response = SMessages.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sMessagesService.deleteById(id);
        return restReturnResult;
    }


}

